<%--
  Created by IntelliJ IDEA.
  User: 15890741124
  Date: 2022/8/9
  Time: 14:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>详细信息</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
</head>
<body>
<%--查询--%>
<form class="layui-form" action="" style="margin-top: 20px">
    <div class="layui-inline">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-block">
            <input type="text" name="question" placeholder="请输入问题" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" name="createBy" placeholder="请输入创建人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
            <button class="layui-btn layui-btn-primary" id="reset" type="reset">重置</button>
        </div>
    </div>
</form>
<%--表格--%>
<table id="demo" lay-filter="test"></table>
<%--表格右侧标签--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除 </a>
</script>
<%--表格头标签--%>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="del">删除</button>
    </div>
</script>
<%-- 添加表单  --%>
<form class="layui-form" action="" id="addForm" style="display: none" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">请输入id</label>
        <div class="layui-input-block">
            <input type="text" name="id" lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题描述</label>
        <div class="layui-input-block">
            <input type="text" name="question" required lay-verify="required" placeholder="请输入问题描述" autocomplete="off"
                   class="layui-input" id="questionAdd"><span style="color: red"></span>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <input type="text" name="optionA" lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <input type="text" name="optionB" lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <input type="text" name="optionC" lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <input type="text" name="optionD" lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题类型</label>
        <div class="layui-input-block">
            <select name="type" lay-verify="required">
                <option value="1">前15题</option>
                <option value="2">后15题</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" value="0" checked lay-skin="switch" lay-text="开启|关闭">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addUserFilter" id="addQuestion">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%-- 修改表单 根据ID修改问题--%>
<form class="layui-form" action="" lay-filter="updateFromFilter" id="updateForm" style="display: none">
    <input type="hidden" name="id" id="id">
    <div class="layui-form-item">
        <label class="layui-form-label">问题描述</label>
        <div class="layui-input-block">
            <input type="text" name="question" required lay-verify="required" placeholder="请输入问题描述" autocomplete="off"
                   class="layui-input" id="questionUpdate"><span style="color: red"></span>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <input type="text" name="optionA" lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <input type="text" name="optionB" lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <input type="text" name="optionC" lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <input type="text" name="optionD" lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题类型</label>
        <div class="layui-input-block">
            <select name="type" lay-verify="required">
                <option value="1">前15题</option>
                <option value="2">后15题</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" value="0" checked lay-skin="switch" lay-text="开启|关闭">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateUserFilter" id="upQuestion">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script>

    layui.use(['table', 'form', 'upload'], function () {
        var table = layui.table;
        var form = layui.form;
        var upload = layui.upload;
        table.render({
            elem: '#demo'
            , height: 550
            , url: '${pageContext.request.contextPath}/fpa/query.do'
            , dataType: 'json'
            , page: true //开启分页
            // ,toolbar:'default'
            , limits: [5, 10, 15, 20, 25, 30, 100, 200]//将每页几个写在这里，不想默认10个每页
            , autoSort: true//关闭自动排序
            , cols: [[
                {checkbox: 'checkbox', fixed: 'left'}//左侧复选框
                , {field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left'}
                , {field: 'question', title: '问题', width: 180}
                , {field: 'optionA', title: '问题一', width: 180}
                , {field: 'optionB', title: '问题二', width: 180}
                , {field: 'optionC', title: '问题三', width: 180}
                , {field: 'optionD', title: '问题四', width: 180}
                , {
                    field: 'type', title: '类型', width: 120,
                    templet: function (d) {
                        //得到当前行数据，并拼接成自定义模板
                        if (d.type == 1) {
                            //return '可用'
                            return 'FPA前15题'
                        } else if (d.type == 2) {
                            return 'FPA后15题'
                        } else {
                            return '未知类型'
                        }

                    }
                }
                , {
                    field: 'status', title: '状态', width: 80,
                    templet: function (d) {
                        //得到当前行数据，并拼接成自定义模板
                        if (d.status == 0) {
                            //return '可用'
                            return '<a class="layui-btn  layui-btn-xs">可用</a>'
                        } else {
                            return '<a class="layui-btn layui-btn-disabled  layui-btn-xs">失效</a>'
                        }

                    }
                }
                , {field: 'createTime', title: '时间', width: 200}
                , {field: 'createBy', title: '创建人', width: 200}
                , {field: 'updateTime', title: '修改时间', width: 200}
                , {field: 'updateBy', title: '修改人', width: 200}
                , {field: 'flag', title: 'flag', width: 200}
                //右侧操作栏
                , {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barDemo'}
            ]]
            , toolbar: "#toolbarDemo"//头部操作栏
        });



        //触发头部工具栏
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case "del":
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //定义一个数组，封装所有的id
                        var ids = [];
                        //循环所有选中的行
                        for (let i = 0; i < data.length; i++) {
                            ids[i] = data[i].id;
                        }
                        layer.confirm('真的删除行么', function (index) {
                            $.ajax({
                                url: "${pageContext.request.contextPath}/fpa/delete.do",
                                data: {id: ids.join(",")},
                                //响应的数据类型
                                dataType: "JSON",
                                type: "POST"
                                //异步请求
                                // async: true,
                                //成功的回调
                                , success: function (data) {
                                    if (data.code == 0) {
                                        layer.closeAll();//关闭所有
                                        //刷新表格数据
                                        table.reload('demo', {
                                            page: {
                                                curr: 1
                                                //重新从第 1 页开始
                                            }
                                        });

                                    }
                                    layer.msg(data.msg);
                                }
                            });

                        });
                    }
                    break;
                case "add":
                    layer.open({//弹出添加框
                        type: 1,
                        content: $("#addForm"),
                        area: ('500px', '600px')
                    });
                    break;
                default:
                    break;
            }
        });

        //提交添加页面
        form.on('submit(addUserFilter)', function (data) {
            if (data.field.status === '0') {
                data.field.status = 0;
            } else {
                data.field.status = 1;
            }
            $.ajax({
                url: "${pageContext.request.contextPath}/fpa/insert.do",
                type: "POST",
                dataType: "JSON",
                //data:data.field,
                //contentType:"application/json;charset=UTF-8",
                data: data.field,
                success: function (ret) {
                    if (ret.code == 0) {
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        table.reload('demo', {
                            page: {
                                curr: 1
                                //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg, {icon: 6});
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }

                }
            })
            document.getElementById("addForm").reset();
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //对问题绑定失去焦点事件，发送ajax请求
        $("#questionAdd").blur(function () {
            $.ajax({
                url: "${pageContext.request.contextPath}/fpa/repetitionQusetion.do",
                data: {question: $("#questionAdd").val()},
                dataType: "json",
                type: "get",
                success: function (re) {
                    if (re.code == 0) {
                        $("#questionAdd").next().html(re.msg);
                        $('#addQuestion').removeClass("layui-btn-disabled").attr("disabled", false);
                    } else {
                        $("#questionAdd").next().html(re.msg);
                        $('#addQuestion').addClass("layui-btn-disabled").attr("disabled", true);
                    }
                }
            })
        })
        //监听右侧工具栏事件
        table.on('tool(test)', function (obj) {
            switch (obj.event) {
                case "del":
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/fpa/delete.do",
                            data: {"id": obj.data.id},
                            //响应的数据类型
                            dataType: "JSON",
                            type: "POST"
                            //异步请求
                            // async: true,
                            //成功的回调
                            , success: function (data) {
                                if (data.code == 0) {
                                    layer.closeAll();
                                    //刷新表格数据
                                    table.reload('demo', {
                                        page: {
                                            curr: 1
                                            //重新从第 1 页开始
                                        }
                                    });
                                }
                              /*  layer.msg(data.msg);*/
                            }
                        });
                    });
                    break;
                case "edit":
                    var id = obj.data.id;
                    var question = obj.data.question;
                    var optionA = obj.data.optionA;
                    var optionB = obj.data.optionB;
                    var optionC = obj.data.optionC;
                    var optionD = obj.data.optionD;
                    //将选择的admin填入到表单中
                    form.val("updateFromFilter", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                        "id": id // "name": "value"
                        ,"question": question // "name": "value"
                       , "optionA": optionA // "name": "value"
                        ,"optionB": optionB // "name": "value"
                        ,"optionC": optionC // "name": "value"
                        ,"optionD": optionD // "name": "value"
                    });
                    layer.open({//弹出修改框
                        type: 1,
                        content: $("#updateForm"),
                        area: ('500px', '600px')
                    });
                    break;
                default:
                    break;
            }
        });
        //提交修改页面
        form.on('submit(updateUserFilter)', function (data) {
            //提交表单数据
            if (data.field.status === '0') {
                data.field.status = 0;
            } else {
                data.field.status = 1;
            }
            $.ajax({
                url: "${pageContext.request.contextPath}/fpa/update.do"
                , data: data.field
                , dataType: "json"
                , type: "POST"
                , success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        table.reload('demo', {
                           /* page: {
                                curr: 1
                                //重新从第 1 页开始
                            }*/
                        });
                    }
                    layer.msg(data.msg);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        /**
         * 修改判断问题是否重复
         * @constructor
         */

        $("#questionUpdate").blur(function (){
            $.ajax({
                url: "${pageContext.request.contextPath}/fpa/repetitionQusetion.do",
                data: {question: $("#questionUpdate").val(), id: $("#id").val()},
                dataType: "json",
                type: "get",
                success: function (re) {
                    if (re.code == 0) {
                        $("#questionUpdate").next().html(re.msg);
                        $('#upQuestion').removeClass("layui-btn-disabled").attr("disabled", false);
                    } else {
                        $("#questionUpdate").next().html(re.msg);
                        $('#upQuestion').addClass("layui-btn-disabled").attr("disabled", true);
                    }
                }
            })
        })


        //查询问题
        form.on('submit(formDemo)', function (data) {
            table.reload('demo', {
                where: {
                    //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    question: data.field.question //排序字段-->查询问题
                    , createBy: data.field.createBy//排序方式 --> 查询创建人
                }
                , page: {
                    curr: 1
                    //重新从第 1 页开始
                },
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        //重置查询条件
        $("#reset").click(function () {
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    question: "" //第一个查询条件
                    , create_by: ""//第二个查询条件
                },
                page: {
                    curr: 1
                    //重新从第 1 页开始
                },
            });
        });

    });

</script>
</html>
